package com.echo.register.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.echo.register.common.result.Result;
import com.echo.register.common.util.MD5;
import com.echo.register.hosp.service.HospitalSetService;
import com.echo.register.model.hosp.HospitalSet;
import com.echo.register.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @ClassName: HospitalSetController
 * @Package: com.echo.register.hosp.controller
 * @Description: 医院设置管理
 * @Author 谢同学
 * @Create 2023/12/10 16:39
 * @Version 1.0
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
// @CrossOrigin //解决跨域问题
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;
    /**
     * 获取所有医院设置
     *
     * @return
     */
    @ApiOperation("获取所有医院设置")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> findAllHospital() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 逻辑删除医院设置
     *
     * @param id 医院id
     * @return 是否删除成功
     */
    @ApiOperation("逻辑删除医院设置")
    @ApiImplicitParam(name = "id", value = "医院id", dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public Result<?> removeHospSet(@PathVariable Long id) {
        if (hospitalSetService.removeById(id)) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 批量删除医院设置
     *
     * @param ids 医院id集合
     * @return 是否删除成功
     */
    @ApiOperation(value = "批量删除医院设置")
    @ApiImplicitParam(name = "ids", value = "医院id", paramType = "body", dataType = "List")
    @DeleteMapping("/batchRemove")
    public Result<?> batchRemoveHospitalSet(@RequestBody List<Long> ids) {
        return hospitalSetService.removeByIds(ids) ? Result.ok() : Result.fail();
    }

    /**
     * 分页条件查询
     *
     * @param current            当前页
     * @param limit              每页条数
     * @param hospitalSetQueryVo 查询条件（非必填）
     * @return 查询结果
     */
    @ApiOperation("分页条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "hospitalSetQueryVo", value = "分页条件", dataType = "HospitalSetQueryVo" , paramType = "body")
    })
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHospSet(@PathVariable Long current,
                                                     @PathVariable Long limit,
                                                     @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);//分页参数
        //构建查询条件
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();

  /*      //获取前端传来的条件
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号

        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like(HospitalSet::getHosname, hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq(HospitalSet::getHoscode, hoscode);
        }*/

        if (!StringUtils.isEmpty(wrapper)) {
            wrapper.like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()), HospitalSet::getHosname, hospitalSetQueryVo.getHosname())
                    .eq(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()), HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        return Result.ok(pageHospitalSet);
    }

    /**
     * 添加医院设置
     *
     * @param hospitalSet 医院信息
     * @return
     */
    @ApiOperation("添加医院设置")
    @ApiImplicitParam(name = "hospitalSet", value = "医院信息", dataType = "HospitalSet", paramType = "body")
    @PostMapping("saveHospitalSet")
    public Result<?> saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态
        hospitalSet.setStatus(1);
        //签名密钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000)));
        if (hospitalSetService.save(hospitalSet)) {
            return Result.ok();
        }
        return Result.fail();
    }

    //5 根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @ApiImplicitParam(name = "id", value = "医院id", dataType = "Long", paramType = "path")
    @GetMapping("/getHospSet/{id}")
    public Result<HospitalSet> getHospSet(@PathVariable Long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    //6 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @ApiImplicitParam(name = "hospitalSet", value = "医院信息", dataType = "HospitalSet", paramType = "body")
    @PostMapping("/updateHospitalSet")
    public Result<?> updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        if (hospitalSetService.updateById(hospitalSet)) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation(value = "锁定/解锁医院设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "path")
    })
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result<?> lockHospitalSet(@PathVariable Long id,
                                     @PathVariable Integer status) {
        //根据id查询状态
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();
    }

    //发送签名秘钥
    @ApiOperation(value = "发送签名key")
    @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path")
    @PutMapping("/sendKey/{id}")
    public Result<?> sendKey(@PathVariable Long id) {
        //根据id查询签名
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();//编号
        String signKey = hospitalSet.getSignKey();//签名key
        //发送签名


        return Result.ok();
    }

}
